package com.texaco.chapasFutbol.scene;

import java.util.ArrayList;
import java.util.List;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.AutoWrap;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.font.Font;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.progress.IProgressListener;

import com.texaco.chapasFutbol.ChapasFutbolActivity;
import com.texaco.chapasFutbol.R;
import com.texaco.chapasFutbol.buttons.Button;
import com.texaco.chapasFutbol.buttons.IButtonCallBack;
import com.texaco.chapasFutbol.entidades.Game;
import com.texaco.chapasFutbol.resources.Resources;

/**
 * Clase que implementa la escena en la que se mostrarán los créditos.
 * @author Manuel
 */
public class CreditScene extends SceneBase{

	private final static int SCROLL_PACE = 1;
	private final static int POS_INICIAL = 50;
	private final static int GAP = 60;
	
	private ChapasFutbolActivity activity;
	private List<Text> texts;

	public CreditScene(Game game){
		super(game);
		this.activity = game.getActivity();
	}

	@Override
	public void initAsync(IProgressListener pProgressListener) {
		pProgressListener.onProgressChanged(0);

		Sprite bgCommon = new Sprite(0, 0, 
				activity.getShapesMaps().get(Game.OPTIONS_BACKGROUND_SHAPE), 
				activity.getVertexBufferObjectManager());
		setBackground(new SpriteBackground(bgCommon));

		final int MARGEN = 10;
		int pos = 1;
		
		final TextOptions textOptions = new TextOptions(AutoWrap.WORDS, 
				activity.getEngine().getCamera().getWidth(), 
				HorizontalAlign.CENTER, 0);
		
		CharSequence[] creditsTexts = activity.getResources().getTextArray(R.array.a_credits);
		
		this.texts = new ArrayList<Text>();
		
		for(int i = 0; i< creditsTexts.length; i++){
			Text text = new Text(0, 0, 
					activity.getFontMap().get(Resources.FONT_OPTIONS_CAPTION), 
					creditsTexts[i], 500,
					textOptions,
					activity.getVertexBufferObjectManager());
			attachChild(text);
			texts.add(text);
		}
		
		initText(texts);
 
		pProgressListener.onProgressChanged(50);

		int textId = R.string.tLabelButtonBack;
		Font buttonFont = activity.getFontMap().get(Resources.FONT_OPTIONS);
		Button bBack = new Button(game, this, 0, 0, textId, buttonFont,
				Game.BOTON_SIGUIENTE_SHAPE, new IButtonCallBack() {
					public String fire() {
						activity.getSoundHandler()
								.playFx(Resources.SOUND_CLICK);
						game.loadOptionScene();
						return null;
					}
				});
		
		Camera cam = activity.getEngine().getCamera();
		float rightMargin = activity.getResources().getInteger(R.integer.layout_right_margin);
		float bottomMargin = activity.getResources().getInteger(R.integer.layout_bottom_margin);
		
		bBack.setPosition(cam.getWidth() - rightMargin - bBack.getWidth(), 
				cam.getHeight() - bottomMargin - bBack.getHeight());
		
		pProgressListener.onProgressChanged(100);
		this.registerUpdateHandler(new TimerHandler(0.05f, true, new ITimerCallback() {
			public void onTimePassed(TimerHandler pTimerHandler) {
				if(texts.get(texts.size() - 1).getY() < 0){
					initText(texts);
				}else{
					for(Text text : texts){
						text.setPosition(text.getX(), text.getY() - SCROLL_PACE);
					}
				}
			}
		}));
	}

	@Override
	protected void update() {
	}

	private void initText(List<Text> texts){
		int i = 0;
		for(Text text : texts){
			text.setPosition(0, POS_INICIAL + (i++ * GAP));
		}
	}

	@Override
	protected boolean async() {
		return true;
	}
}
